package com.example.badmintonDB.Server;

import com.example.badmintonCommon.entity.Notice;
import com.example.badmintonCommon.entity.Gym;
import com.example.badmintonCommon.entity.Order;
import com.example.badmintonCommon.entity.User;
import com.example.badmintonCommon.pojo.Result;
import com.example.badmintonDB.AdminDBServer;
import com.example.badmintonDB.dao.AdminMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ibatis.session.SqlSessionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminDBServerImpl implements AdminDBServer {
    private final AdminMapper adminMapper;
    private static final ObjectMapper objectMapper = new ObjectMapper();
    @Autowired
    public AdminDBServerImpl(AdminMapper adminMapper){
        this.adminMapper=adminMapper;
    }



    //添加场地信息
    @Override
    public void AddGym(Gym gym){
        adminMapper.AddGym(gym);
    }

    //添加场地信息
    @Override
    public Integer  getMaxId(){
        return adminMapper.getMaxId();
    }

    //删除场地信息
    @Override
    public void DeleteGymById(Integer id){
        adminMapper.DeleteGymById(id);
    }

    //显示所有用户（管理员除外）
    @Override
    public List<User> FindAllUsers(String role){
        return adminMapper.FindAllUsers(role);
    }

    //增加公告
    @Override
    public void AddNotice(Notice notice) {
        adminMapper.AddNotice(notice);
    }

    //更新公告
    @Override
    public void UpdateNotice(Notice notice) {
        Notice old = adminMapper.FindNoticeById(notice.getId());
        if (old == null) {
            throw new RuntimeException("公告不存在");
        }
        adminMapper.UpdateNotice(notice);
    }

    //找所有公告
    @Override
    public List<Notice> FindAllNotices() {
        return adminMapper.FindAllNotices();
    }

    //通过id找公告
    @Override
    public Notice FindNoticeById(Integer id) {
        return adminMapper.FindNoticeById(id);
    }

    @Override
    public List<Order> getOrders() {
        return adminMapper.getOrders();
    }

    @Override
    public List<Gym> getGyms() {
        return adminMapper.getGyms();
    }

    @Override
    public Result<String> changePlacePrice(int place, int price) {
        try{
            adminMapper.changePlacePrice(place,price);
        }catch (SqlSessionException e){
            return Result.error("删除失败:"+e);
        }
        return Result.success("删除成功");

    }

    @Override
    public Result<String> lockOrUnlockUser(String tel) {
        try {
            adminMapper.lockOrUnlockUser(tel);
        }catch (SqlSessionException e){
            return Result.error("修改失败："+e);
        }
        return Result.success();


    }

    @Override
    public Result<String> deleteNotice(int id) {

        try {
            adminMapper.deleteNotice(id);
        }catch (SqlSessionException e){
            return Result.error("修改失败："+e);
        }
        return Result.success();


    }


    @Override
    public String GetNoticeIsdelete(int id) {
             return adminMapper.GetNoticeIsdelete(id);
     }
}
